﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using Numbers;
using System.Linq;
using System.Runtime.Serialization;

namespace System8ToyMC
{
    /// <summary>
    /// Help with isolated storage
    /// </summary>
    public class DataLoadSaveHelper
    {
        public ObservableCollection<string> LocalDatasets { get; private set; }

        public DataLoadSaveHelper()
        {
            LocalDatasets = new ObservableCollection<string>();
            UpdateListOfItems();
        }

        public struct SavedData
        {
            public struct SavedDataFlavorInfo
            {
                public SavedDataFlavorInfo(FlavorSample s)
                {
                    b = s.Bottom;
                    c = s.Charm;
                    l = s.Light;
                }
                public double b, c, l;

                /// <summary>
                /// Restore the data!
                /// </summary>
                /// <param name="flavorSample"></param>
                internal void Restore(FlavorSample fs)
                {
                    fs.Bottom = b;
                    fs.Charm = c;
                    fs.Light = l;
                }
            }

            public SavedDataFlavorInfo n, p, n1, n2, p1, p2, n12, p12;
        }

        /// <summary>
        /// Save the dataset to local storage.
        /// </summary>
        /// <param name="setName"></param>
        /// <param name="eqns"></param>
        public void SaveCurrentState(string setName, System8Equations.S8Equations eqns)
        {
            ///
            /// Convert to an object
            /// 

            SavedData tosave;
            tosave.n = new SavedData.SavedDataFlavorInfo(eqns.n);
            tosave.p = new SavedData.SavedDataFlavorInfo(eqns.p);
            tosave.n1 = new SavedData.SavedDataFlavorInfo(eqns.n1);
            tosave.n2 = new SavedData.SavedDataFlavorInfo(eqns.n2);
            tosave.p1 = new SavedData.SavedDataFlavorInfo(eqns.p1);
            tosave.p2 = new SavedData.SavedDataFlavorInfo(eqns.p2);
            tosave.n12 = new SavedData.SavedDataFlavorInfo(eqns.n12);
            tosave.p12 = new SavedData.SavedDataFlavorInfo(eqns.p12);

            var iso = IsolatedStorageFile.GetUserStoreForApplication();
            if (!iso.DirectoryExists("datasets"))
            {
                iso.CreateDirectory("datasets");
            }
            using (var f = iso.CreateFile("datasets/" + setName + ".dataset"))
            {
                DataContractSerializer ds = new DataContractSerializer(typeof(SavedData));
                ds.WriteObject(f, tosave);
            }

            UpdateListOfItems();
        }

        /// <summary>
        /// Load the eqns from the given dataset.
        /// </summary>
        /// <param name="setName"></param>
        /// <param name="eqns"></param>
        public void LoadCurrentState(string setName, System8Equations.S8Equations eqns)
        {
            var iso = IsolatedStorageFile.GetUserStoreForApplication();
            using (var f = iso.OpenFile("datasets\\" + setName + ".dataset", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                DataContractSerializer ds = new DataContractSerializer(typeof(SavedData));
                SavedData result = (SavedData)ds.ReadObject(f);

                result.n.Restore(eqns.n);
                result.p.Restore(eqns.p);
                result.n1.Restore(eqns.n1);
                result.p1.Restore(eqns.p1);
                result.n2.Restore(eqns.n2);
                result.p2.Restore(eqns.p2);
                result.n12.Restore(eqns.n12);
                result.p12.Restore(eqns.p12);
            }
        }

        /// <summary>
        /// Get a list of the files and update our internal list.
        /// </summary>
        private void UpdateListOfItems()
        {
            var iso = IsolatedStorageFile.GetUserStoreForApplication();
            LocalDatasets.Clear();
            if (!iso.DirectoryExists("datasets"))
            {
                return;
            }

            var files = from f in iso.GetFileNames("datasets\\*.dataset")
                        let baseFilename = System.IO.Path.GetFileName(f)
                        select System.IO.Path.ChangeExtension(baseFilename, null);
            foreach (var f in files)
            {
                LocalDatasets.Add(f);
            }
        }
    }
}
